
package com.yimixian.app.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;

public class Strings {

    public static boolean isNullOrEmpty(String str) {
        return str == null || str.length() == 0;
    }

    public static String nullToEmpty(String str) {
        return str == null ? "" : str;
    }

    public static String join(Iterable iterable, String delimiter) {
        if (iterable == null) {
            return "";
        }

        StringBuilder builder = new StringBuilder();
        int i = 0;
        for (Object object : iterable) {
            if (i++ > 0) {
                builder.append(delimiter);
            }
            builder.append(object.toString());
        }

        return builder.toString();
    }

    public static String repeat(String str, int num) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < num; ++i) {
            builder.append(str);
        }
        return builder.toString();
    }

    public static String inputStream2String(InputStream is) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int i = -1;
        try {
            while ((i = is.read()) != -1) {
                baos.write(i);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return baos.toString();
    }

    public static String getDecimalWith2Number(Double num) {
        DecimalFormat df = new DecimalFormat("###,##0.00");
        return df.format(num);
    }
    public static String ToSBC(String input) {
        char[] c = input.toCharArray();
        for (int i = 0; i< c.length; i++) {
            if (c[i] == 12288) {
                c[i] = (char) 32;
                continue;
            }if (c[i]> 65280&& c[i]< 65375)
                c[i] = (char) (c[i] - 65248);
        }
        return new String(c);
    }
}
